import os
import sys

from flask import Flask, request

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print("BASE_DIR: ", BASE_DIR)
sys.path.append(BASE_DIR)

from a_pychon_scripts.redis_conf import r

app = Flask(__name__)


@app.route("/index")
def index():
    if request.method == "GET":
        args = request.args
        print("args: ", args)
        # 简单的模拟限流逻辑：
        if r.exists("rate_limit"):
            ret = {
                "code": 50000,
                "msg": "request rate limit!!!"
            }
            return ret
        ret = {
            "code": 10000,
            "data": {
                "value": args.get("value"),
            }
        }
        # 接口限流的标志
        r.set("rate_limit", "OK", ex=3)
        return ret


if __name__ == '__main__':
    app.run("127.0.0.1", 9000)
